"""
Plotting functionality.

"""
from const import PNG

def save( chart, fname):
    "Saves a chart to a file."
    chart.makeChart( fname )

def show( chart ):
    """
    Helper class that displays chart image in a Tk window. 
    Clicking on the image quits the application. Requires the PIL library.
    """
    import Tkinter, StringIO
    from PIL import ImageTk, Image

    output = StringIO.StringIO()
    output.write( chart.makeChart2( PNG ) )
    output.seek(0) # rewind
    root  = Tkinter.Tk()
    image = ImageTk.PhotoImage( image=Image.open( output ) )
    Tkinter.Button(image=image, command=root.quit).pack()
    root.mainloop()

def test( verbose=0 ):
    "Performs module level testing"
    import doctest
    doctest.testmod( optionflags=doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE, verbose=verbose )

if __name__ == "__main__":
    test()
